﻿package net.wangit.dev.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.dev.LabelRule;
import net.wangit.dev.ReferenceUtil;
import net.wangit.framework.expression.LabelExpression;
import net.wangit.jdbc.DB;
import net.wangit.util.Checker;
import net.wangit.view.tree.Tree;

public class UnitTree extends ObjectTree {

	private static final String ICON_PATH = "/domain/dev/u/";
	private static final String PREFIX = "_02C";
	private static final String TYPE = Integer.toString(2);
	private static final String COLUMNS = "select uid,ti,pid,pp,l,y,c,au,dpid,gid,o";
	// private static final String UNITID_WITH_PARENT = " (pid is not null or
	// pid!='') and pid in (select uid pid from UNIT ) ";
	private static final String SQL_UNIT = " from UNIT  where uid not in (select uid  from UNIT where   (pid is not null or pid!='')  and  pid in (select uid pid from UNIT ) ) ";
	private static final String SQL_UNIT_WITH_PARENT = " from UNIT where  (pid is not null or pid!='')  and  pid in (select uid pid from UNIT ) ";
	private static final String SQL_ORDER = " order by o,uid";
	private static final String COUNT_NAME = "rowcount";
	private static final String ITEM_SQL = "select count(*) rowcount from ELEMENT where uid=?";
	private MapList children;

	public UnitTree() {
	}

	protected String getItemsTableName() {
		return "ELEMENT";
	}

	protected String getComponentType() {
		return TYPE;
	}

	protected MapList getObjects(DB db) throws Exception {
		MapList objects = db.query(getSql(UnitTree.SQL_UNIT));
		children = db.query(getSql(UnitTree.SQL_UNIT_WITH_PARENT));
		objectCount = (Checker.isEmpty(objects) ? 0 : objects.size())
				+ (Checker.isEmpty(children) ? 0 : children.size());
		return objects;
	}

	protected boolean showNullGroup() {
		return true;
	}

	protected String getGroupAttribute() {
		return "oncontextmenu=wmg2()";
	}

	protected int addObject(ActionContext ac, DB db, Tree parent, Row row)
			throws Exception {
		int itemCount = 0;
		Tree object = new Tree();
		String unitId = row.get("uid");
		int c = row.getInt("c", 1);
		object.setId((new StringBuilder(UnitTree.PREFIX)).append(unitId)
				.toString());
		String name = LabelExpression.resolve(ac, row.get("ti"));
		object.setIcon((new StringBuilder(UnitTree.ICON_PATH)).append(
				getIcon(db, unitId, row)).toString());
		if (check) {
			setUnit(ac, object, db, unitId, name, row);
		} else {
			int referenceCount = ReferenceUtil.getReferenceCount(db, Integer
					.toString(80), unitId);
			itemCount = getItemCount(c, db, unitId);
			String label = LabelRule.getLabel(ac, ObjectTreeHelper
					.getLableRule(ac), unitId, name);
			if (c == 4 || c == 7)
				object.setName((new StringBuilder(String
						.valueOf(referenceCount))).append(" ").append(label)
						.toString());
			else
				object.setName((new StringBuilder(String
						.valueOf(referenceCount))).append(" ").append(label)
						.append(" ").append(itemCount).toString());
			object.setAttribute(getContextMenu(c));
			object.setAction(getUrl(c, unitId));
		}
		itemCount += addChildren(ac, db, object, unitId);
		add2Parent(parent, object);
		return itemCount;
	}

	private String getContextMenu(int c) {
		switch (c) {
		case 1:
		case 2:
		case 3:
			return "oncontextmenu=wm2()";

		case 4:
			return "oncontextmenu=wmfs()";

		case 7:
			return "oncontextmenu=wmjr()";

		case 5:
		case 6:
		default:
			return "";
		}
	}

	private String getUrl(int c, String unitId) {
		switch (c) {
		case 1:
		case 2:
		case 3:
			return (new StringBuilder("/dev/unit.do?m=e&uid=")).append(unitId)
					.toString();

		case 4:
			return (new StringBuilder("/dev/frameset.do?m=e&uid=")).append(
					unitId).toString();

		case 7:
			return (new StringBuilder("/dev/jr.do?m=e&uid=")).append(unitId)
					.toString();

		case 5:
		case 6:
		default:
			return "";
		}
	}

	private int addChildren(ActionContext ac, DB db, Tree parent, String unitId)
			throws Exception {
		int elementCount = 0;
		if (!Checker.isEmpty(children)) {
			for (int i = 0; i < children.size(); i++) {
				Row row = children.getRow(i);
				if (unitId.equals(row.get("pid")))
					elementCount += addObject(ac, db, parent, row);
			}

			return elementCount;
		} else {
			return 0;
		}
	}

	private String getSql(String sql) {
		StringBuffer result = new StringBuffer(UnitTree.COLUMNS);
		result.append(getAppendColumns()).append(sql).append(getAppendLimit())
				.append(UnitTree.SQL_ORDER);
		return result.toString();
	}

	private int getItemCount(int c, DB db, String unitId) throws Exception {
		switch (c) {
		case 1:
		case 2:
		case 3:
			MapList list = db.query(UnitTree.ITEM_SQL, unitId, 2);
			return list.getRow(0).getInt(UnitTree.COUNT_NAME, 0);

		case 4:
		case 7:
			return 0;

		case 5:
		case 6:
		default:
			return 0;
		}
	}

	protected String getIcon(DB db, String unitId, Row row) throws Exception {
		return ObjectTreeHelper.getUnitIcon(db, unitId, row, true);
	}

	protected String getAppendColumns() {
		return "";
	}

	protected String getAppendLimit() {
		return "";
	}

	protected void add2Parent(Tree parent, Tree object) {
		parent.add(object);
	}

	protected void setUnit(ActionContext ac, Tree object, DB db, String unitId,
			String name, Row row) throws Exception {
		object.setName((new StringBuilder(String.valueOf(unitId))).append(" ")
				.append(name).toString());
	}

}
